//
//
//
//


#include <openrave-core.h>
#include <vector>
#include <cstring>
#include <sstream>
#include <openrave/planningutils.h>

#include "orexample.h"

using namespace OpenRAVE;
using namespace std;



void demothreadwrapper(int argc, char ** argv) {
    boost::shared_ptr<void> quitviewer((void*)NULL, boost::bind(&quitviewer, this,_1));
    demothread(argc,argv);
}


void quitviewer(void *) {
    if( !!viewer ) {
        viewer->quitmainloop();
    }
}

void demothreadwrapper(int argc, char ** argv) {
    boost::shared_ptr<void> quitviewer((void*)NULL, boost::bind(&quitviewer, this,_1));
    demothread(argc,argv);
}

static void sigint_handler(int sig)
{
    if( !!GetSingleton() ) {
        GetSingleton()->Exit();
    }
}

static OpenRAVEExample*& GetSingleton() {
    static OpenRAVEExample* psingleton = NULL;
    return psingleton;
}






int main(int argc, char ** argv) {


	std::string 				viewername;
    OpenRAVE::ViewerBasePtr 	viewer;
    bool 						bDestroyThread;
    boost::thread 				thopenrave;


	OpenRAVE::RaveInitialize(true);

	OpenRAVE::EnvironmentBasePtr penv = OpenRAVE::RaveCreateEnvironment();
	penv->SetDebugLevel(OpenRAVE::Level_Debug);

	// create a viewer
	viewer.reset();
	if( viewername.size() > 0 ) {
		viewer = OpenRAVE::RaveCreateViewer(penv, viewername);
	}
	if( !!viewer ) {
		penv->Add(viewer);

		// create the main openrave thread
		bDestroyThread = false;
		thopenrave = boost::thread(boost::bind(&OpenRAVEExample::demothreadwrapper, this, argc, argv));

		// start the viewer main loop
		viewer->main(true);
	}
	else {
		// just execute
		demothread(argc,argv);
	}
	return 0;
}


